<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Model
{
    public function __construct() {
        
    }
    
    public function executeAll($query, $params=array(), $isPaging=true)
    {
        if($isPaging)
        {
            $start = 0;
            $limit = Yii::app()->params['recordsPerPage'];
            
            if(isset($_GET['start']))
            {
                $start = $_GET['start'] + 0;
            }
            
            if(isset($_GET['limit']))
            {
                $limit = $_GET['limit'] + 0;
            }
            
            $query .= " LIMIT :start, :limit";
            
            $params[':start'] = $start;
            $params[':limit'] = $limit;
        }
        
        $command = Yii::app() -> db -> createCommand($query);
        $command ->bindValues($params);
        
        $result = $command ->queryAll() ;
        
        return $result; 
    }
    
    public function executeAllTotal($query, $params=array(), $isPaging=true)
    {
        if($isPaging)
        {
            $start = 0;
            $limit = 25;
            
            if(isset(Yii::app()->params['recordsPerPage']))
                $limit = Yii::app()->params['recordsPerPage'];
            
            if(!stristr($query, 'sql_calc_found_rows'))
            {
                $temp = preg_split('/^\w*/', $query);
                $query = "SELECT sql_calc_found_rows " . $temp[1];  
            }
            
            if(isset($_GET['start']))
            {
                $start = $_GET['start'] + 0;
            }
            
            if(isset($_GET['limit']))
            {
                $limit = $_GET['limit'] + 0;
            }
            
            $query .= " LIMIT :start, :limit";
            
            $params[':start'] = $start;
            $params[':limit'] = $limit;
        }
        
        $command = Yii::app() -> db -> createCommand($query);
        $command ->bindValues($params);
        
        $result = $command ->queryAll();
        
        $query = "SELECT found_rows()";
        $command = Yii::app() -> db -> createCommand($query);
        
        $total = $command ->queryScalar();
        
        return array($result, $total); 
    }
    
    public function executeScalar($query, $params=array())
    {
        $command = Yii::app() -> db -> createCommand($query);
        $command ->bindValues($params);
        $result = $command ->queryScalar() ;
        
        return $result; 
    }
    
    public function executeRow($query, $params=array())
    {
        $command = Yii::app() -> db -> createCommand($query);
        $command ->bindValues($params);
        $result = $command ->queryRow() ;
        
        return $result; 
    }
    
    public function executeNone($query, $params=array())
    {
        $command = Yii::app() -> db -> createCommand($query);
        $command ->bindValues($params);
        $result = $command ->execute();
        
        return $result; 
    }
}